package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo36 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result = 0;
                for (int i = 1; i <= 1000; i++) {
                    result += i;
                }
                //不需要引入成员变量
                //直接借助这里地返回值即可
                return result;
            }
        };
        //引入FutureTask来作为thread和callable的粘合剂
        FutureTask<Integer> futureTask = new FutureTask<>(callable);//未来的任务
        Thread t = new Thread(futureTask);//Thread没有构造方法来传入callable
        t.start();

        //也是带有阻塞功能的
        System.out.println(futureTask.get());
    }
}
